package study.java.jdk.jdk8._1_stream.sort;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import study.java.jdk.jdk8.User1;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class sorted_Test {

    List<String> src = Arrays.asList("2021-04-03", "2021-04-05", "2021-04-01");

    /**
     * 使用自然顺序对流的元素进行排序。元素类必须实现Comparable接口。
     *
     * @param
     */
    @Test
    public void test1() {
        List<String> dest = src.stream()
                .sorted()
                .collect(Collectors.toList());
        log.info("{}", dest);
    }

    /**
     * @param comparator -Comparator: 比较器
     */
    @Test
    public void test2() {
        List<String> dest = src.stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
        log.info("{}", dest);
    }

    /**
     * 对 Map value 排序
     */
    @Test
    public void test3() {
        User1 u1 = User1.builder().code("1").age(30).build();
        User1 u2 = User1.builder().code("1").age(36).build();
        User1 u3 = User1.builder().code("2").age(46).build();
        User1 u4 = User1.builder().code("2").age(40).build();
        List<User1> src = Arrays.asList(u1, u2, u3, u4);

        //分组
        Map<String, List<User1>> dest = src.stream()
                .collect(Collectors.groupingBy(User1::getCode));
        log.info("{}", dest);
        //排序
        dest.values()
                .forEach(e -> Collections.sort(e, Comparator.comparingInt(User1::getAge).reversed()));
        log.info("{}", dest);
    }
    //对象集合排序
    //数字排序
    //字符串排序
}
